package com.benti.graphic.heatmap;

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;

import javax.imageio.ImageIO;

import com.sun.media.jai.codecimpl.PNGImageEncoder;

import junit.framework.TestCase;

public class HeatMapTest {
	private static long totalCost;
	private static int count;

	public static void main(String[] args) {
		final int[][] s = { { 279, 112 }, { 276, 60 }, { 388, 308 },
				{ 306, 321 }, { 147, 214 }, { 273, 465 }, { 273, 149 },
				{ 114, 439 }, { 52, 237 }, { 19, 433 }, { 170, 203 },
				{ 210, 472 }, { 28, 51 }, { 40, 10 }, { 176, 147 }, { 75, 83 },
				{ 258, 122 }, { 137, 284 }, { 436, 229 }, { 438, 395 },
				{ 239, 432 }, { 109, 43 }, { 318, 42 }, { 193, 427 },
				{ 386, 472 }, { 23, 19 }, { 231, 153 }, { 294, 382 },
				{ 226, 341 }, { 253, 420 }, { 220, 220 }, { 241, 453 },
				{ 57, 295 }, { 438, 395 }, { 239, 432 }, { 109, 43 },
				{ 318, 42 }, { 193, 427 }, { 386, 472 }, { 23, 19 },
				{ 231, 153 }, { 294, 382 }, { 226, 341 }, { 253, 420 },
				{ 220, 220 }, { 241, 453 }, { 57, 295 }, { 253, 413 },
				{ 220, 228 }, { 241, 453 }, { 57, 295 }, { 253, 400 },
				{ 220, 220 }, { 241, 453 }, { 57, 295 }, { 253, 413 },
				{ 220, 228 }, { 241, 453 }, { 57, 295 }, { 253, 413 },
				{ 200, 228 }, { 241, 450 }, { 50, 290 }, { 253, 413 },
				{ 210, 220 }, { 241, 453 }, { 59, 295 }, { 253, 413 },
				{ 230, 228 }, { 241, 453 }, { 57, 295 }, { 253, 420 },
				{ 220, 228 }, { 241, 453 }, { 57, 290 }, { 253, 413 },
				{ 220, 228 }, { 241, 453 }, { 57, 295 }, { 221, 61 },
				{ 339, 116 }, { 75, 83 }, { 258, 122 }, { 137, 284 },
				{ 436, 229 }, { 438, 395 }, { 239, 432 }, { 109, 43 },
				{ 318, 42 }, { 193, 427 }, { 386, 472 }, { 23, 19 },
				{ 231, 153 }, { 294, 382 }, { 226, 341 }, { 253, 413 },
				{ 220, 228 }, { 241, 453 }, { 57, 295 }, { 241, 453 },
				{ 57, 295 }, { 221, 61 }, { 339, 116 }, { 75, 83 },
				{ 258, 122 }, { 137, 284 }, { 436, 229 }, { 438, 395 },
				{ 239, 432 }, { 109, 43 }, { 318, 42 }, { 193, 427 },
				{ 386, 472 }, { 23, 19 }, { 231, 153 }, { 294, 382 },
				{ 226, 341 }, { 253, 413 }, { 220, 228 }, { 241, 453 },
				{ 57, 295 }, { 221, 61 }, { 339, 116 }, { 75, 83 },
				{ 258, 122 }, { 137, 284 }, { 436, 229 }, { 438, 395 },
				{ 239, 432 }, { 109, 43 }, { 318, 42 }, { 193, 427 },
				{ 386, 472 }, { 23, 19 }, { 231, 153 }, { 294, 382 },
				{ 226, 341 }, { 253, 413 }, { 220, 228 }, { 241, 453 },
				{ 57, 295 }, { 221, 61 }, { 339, 116 }, { 95, 386 },
				{ 416, 159 }, { 74, 193 }, { 245, 316 }, { 384, 16 },
				{ 153, 322 }, { 319, 311 }, { 133, 224 }, { 282, 296 },
				{ 121, 30 }, { 314, 427 }, { 449, 481 }, { 24, 93 },
				{ 344, 74 }, { 403, 102 }, { 37, 277 }, { 333, 380 },
				{ 229, 63 }, { 405, 401 }, { 99, 163 }, { 62, 136 },
				{ 447, 334 }, { 307, 75 }, { 378, 56 }, { 275, 268 },
				{ 472, 396 }, { 100, 186 }, { 48, 478 }, { 458, 333 } };
		final HeatMap h = new HeatMap();
		final long totalStart = new Date().getTime();
		final String path;
		if (args.length > 0)
			path = args[0];
		else
			path = "/tmp/";
		
		for (int i = 0; i < 1; i++) {
			Thread a = new Thread() {
				public void run() {
					try {
						long start = new Date().getTime();
						BufferedImage img = h.generate(1264, 963, s);
						// BufferedImage srcImg=ImageIO.read(new
						// File("/tmp/a.png"));
						BufferedImage srcImg = HeatMap
								.createAlphaImg(1264, 963);
						HeatMap.OverComposite(srcImg, img, .6f);
						long end = new Date().getTime();
						System.out.println("Thread " + this.getId()
								+ " Image-gen end cost: " + (end - start));
						start = new Date().getTime();
						FileOutputStream out = new FileOutputStream(path
								+ "dest" + this.getId() + ".png");
						PNGImageEncoder encoder = new PNGImageEncoder(out, null);
						encoder.encode(srcImg);
						out.close();
						end = new Date().getTime();
						System.out.println("Thread " + this.getId()
								+ " IO write end cost: " + (end - start));
						count++;
						if (count == 5) {
							System.out.println("Total cost: "
									+ (end - totalStart));
						}
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			};
			a.start();
		}
	}

	public void testGenerate() {

	}

	public void testTestColors() {
		BufferedImage baseImg = new BufferedImage(200, 200,
				BufferedImage.TYPE_INT_RGB);
		int c = imagecolorat(baseImg, 0, 0);
		imagesetpixel(baseImg, 1, 1, Color.BLACK.getRed());
		System.out.println(c);
		System.out.println(Color.BLACK.getRGB());
		System.out.println(Color.WHITE.getRGB());
	}

	protected void imagesetpixel(BufferedImage img, int x, int y, int c) {
		img.setRGB(x, y, c);
	}

	protected int imagecolorat(BufferedImage img, int x, int y) {
		return img.getRGB(x, y);
	}
}
